package com.zx.retrofit2andRxJava2;

import android.os.Bundle;
import android.widget.Toast;

import com.zx.retrofit2andRxJava2.app.NetworkBaseActivity;
import com.zx.retrofit2andRxJava2.http.BaseComApi;
import com.zx.retrofit2andRxJava2.http.BaseEntity;
import com.zx.retrofit2andRxJava2.http.api.OrderService;
import com.zx.retrofit2andRxJava2.http.RetroFactory;
import com.zx.retrofit2andRxJava2.http.api.UserService;
import com.zx.retrofit2andRxJava2.model.Order;
import com.zx.retrofit2andRxJava2.model.User;
import com.zx.retrofit2andRxJava2.viewHolder.ActivityMainHolder;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

public class MainActivity extends NetworkBaseActivity {
    ActivityMainHolder holder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        holder = new ActivityMainHolder(findViewById(R.id.activity_main));
        addDisposable(getUsers());
    }

    public Disposable getUsers() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("id", "1");
        map.put("name", "zx");
        return RetroFactory.getInstance().create(UserService.class).getUser(map).compose(BaseComApi.background())
                .doOnError(Throwable::printStackTrace)
                //优先获取用户信息
                .filter(userBaseEntity -> {
                    return showUser(userBaseEntity);
                })
                .doOnError(Throwable::printStackTrace)
                //以下为获取订单信息
                .flatMap(userBaseEntity -> {
                    map.clear();
                    User user = userBaseEntity.getData();
                    map.put("userid", user.getId());
                    return RetroFactory.getInstance().create(OrderService.class).getOrder(map).compose(BaseComApi.background());
                })
                .doOnError(Throwable::printStackTrace)
                .doOnNext(orderBaseEntity -> {
                    if (orderBaseEntity.isSuccess()) {
                        Order order = orderBaseEntity.getData();
                        showOrderView(order);
                    } else {
                        orderBaseEntity.showMsg(MainActivity.this);
                    }
                })
                //以上为获取订单信息
                .compose(flowableLoading())//进度条展示
                .subscribe();
    }

    /**
     * 解析并判断用户是否存在
     *
     * @param userBaseEntity
     * @return
     */
    private boolean showUser(BaseEntity<User> userBaseEntity) {
        if (userBaseEntity.isSuccess()) {
            User user = userBaseEntity.getData();
            if (user.getId() != null && user.getId().length() > 0) {
                showUserView(user);//用户信息获取成功，展示用户信息
                return true;
            } else {
                Toast.makeText(MainActivity.this, "用户ID为空！！", Toast.LENGTH_LONG).show();
                return false;
            }
        } else {
            userBaseEntity.showMsg(MainActivity.this);
            return false;
        }
    }


    private void showUserView(User user) {
        holder.getUserIdTv().setText(user.getName());
        holder.getUserNameTv().setText(user.getSex());
    }

    private void showOrderView(Order order) {
        holder.getOrderIdTv().setText(order.getId());
        holder.getOrderNameTv().setText(order.getName());
    }
}
